﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HYSoft.BLL;


namespace HYSoft.Web
{
    public partial class edmonitor : System.Web.UI.Page
    {
        public string typename;
        public string photo;
        public string oldaddr;
        public int mindex;
        public string images;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] == null || Context.Session["uid"] == null || Context.Session["uid"] == "")
            {
                Context.Response.Write("<script language=\"javascript\" type=\"text/javascript\">");
                Context.Response.Write("alert('对不起,你登录时间超时,请重新登录');top.location.href=\"index.html\";");
                Context.Response.Write("</script>");
                Context.Response.End();
            }
            mindex = int.Parse(Request.QueryString["id"].ToString());
            if (!IsPostBack)
            {
                chuanganqi();
            }
        }

        protected void chuanganqi()
        {

            DataTable dt = new monitor().GetList("[index] = " + mindex).Tables[0];
            if (dt.Rows.Count > 0)
            {

                this.DropDownList23.SelectedValue = dt.Rows[0]["m_area_name"].ToString();
                this.TextBox3.Value = dt.Rows[0]["addr"].ToString();
                this.TextBox1.Value = dt.Rows[0]["coordinates_x"].ToString() + "," + dt.Rows[0]["coordinates_y"].ToString();
                this.TextBox4.Value = dt.Rows[0]["type"].ToString();
                this.TextBox5.Value = dt.Rows[0]["level"].ToString();
                this.TextBox6.Value = dt.Rows[0]["Population"].ToString();
                this.TextBox7.Value = dt.Rows[0]["Contact_name1"].ToString();
                this.TextBox8.Value = dt.Rows[0]["Contact_mobile1"].ToString();
                this.TextBox9.Value = dt.Rows[0]["Contact_name2"].ToString();
                this.TextBox10.Value = dt.Rows[0]["Contact_mobile2"].ToString();
                this.TextBox11.Value = dt.Rows[0]["Contact_name3"].ToString();
                this.TextBox12.Value = dt.Rows[0]["Contact_mobile3"].ToString();
                this.TextBox13.Value = dt.Rows[0]["Upunit1"].ToString();
                this.TextBox14.Value = dt.Rows[0]["Upcontact_name1"].ToString();
                this.TextBox15.Value = dt.Rows[0]["Upcontact_mobile1"].ToString();
                this.TextBox16.Value = dt.Rows[0]["Upunit2"].ToString();
                this.TextBox17.Value = dt.Rows[0]["Upcontact_name2"].ToString();
                this.TextBox18.Value = dt.Rows[0]["Upcontact_mobile2"].ToString();
                photo = dt.Rows[0]["charvalue1"].ToString();
                oldaddr = dt.Rows[0]["addr"].ToString();
                dt.Clear();
                dt.Dispose();
            }
        }



        protected string tounit1()
        {
            string units1 = "";
            DataTable dt = new user().GetList("[index]=" + Session["uid"]).Tables[0];
            units1 = dt.Rows[0]["unit"].ToString();
            dt.Clear();
            dt.Dispose();
            return units1;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {

            string fileNamee1 = "files/" + FileUpload1.FileName.ToString();
            string fileNamee = FileUpload1.FileName.ToString();
            //string text1 = this.address.Value;
            string text1 = this.DropDownList23.SelectedValue;
            string text3 = this.TextBox3.Value;
            string text4 = this.TextBox4.Value;
            string text5 = this.TextBox5.Value;
            string text6 = this.TextBox6.Value;
            string text7 = this.TextBox7.Value;
            string text8 = this.TextBox8.Value;
            string text9 = this.TextBox9.Value;
            string text10 = this.TextBox10.Value;
            string text11 = this.TextBox11.Value;
            string text12 = this.TextBox12.Value;
            string text13 = this.TextBox13.Value;
            string text14 = this.TextBox14.Value;
            string text15 = this.TextBox15.Value;
            string text16 = this.TextBox16.Value;
            string text17 = this.TextBox17.Value;
            string text18 = this.TextBox18.Value;
            string textx = this.TextBox1.Value.TrimStart().TrimEnd().Split(',')[0];
            string texty = this.TextBox1.Value.TrimStart().TrimEnd().Split(',')[1];

            string typ3 = fileNamee.Substring(fileNamee.LastIndexOf(".") + 1);//上传文件的后缀名
            typ3 = typ3.ToLower();
            int flag1 = 0;
            int flag2 = 0;

            if (String.Equals(oldaddr, text3).ToString() == "false")
            {
                DataTable dt8 = new monitor().GetList("Addr='" + text3 + "'").Tables[0];
                if (dt8.Rows.Count > 0)
                {
                    dt8.Clear();
                    dt8.Dispose();
                    Response.Write("<script>alert('该监测点地址已存在，请确认添加监测点信息正确！');history.back();</script>");
                }
                else
                    flag1 = 1;
            }
            else
                flag1 = 1;
            {
                if (fileNamee == "")
                {
                    images = photo;
                    flag2 = 2;
                }
                else
                {
                    if (typ3 == "gif" || typ3 == "jpg" || typ3 == "png")
                    {
                        FileUpload1.SaveAs(Server.MapPath("monitorimage/") + fileNamee);
                        images = "monitorimage/" + fileNamee + "";
                        flag2 = 1;
                    }
                    else
                    {
                        Response.Write("<script>alert('请选择合适的安装图片格式！');history.back();</script>");
                    }
                }
            }
            if (flag1 == 1 && flag2 == 1)
            {
                Model.monitor monitormodel = new monitor().GetModel(mindex);
                if (monitormodel != null)
                {
                    monitormodel.m_area_name = text1; monitormodel.addr = text3; monitormodel.type = text4; monitormodel.level = text5; monitormodel.population = Convert.ToInt32(text6); monitormodel.contact_name1 = text7; monitormodel.contact_mobile1 = text8; monitormodel.contact_name2 = text9; monitormodel.contact_mobile2 = text10; monitormodel.contact_name3 = text11; monitormodel.contact_mobile3 = text12; monitormodel.upunit1 = text13; monitormodel.upcontact_name2 = text14; monitormodel.upcontact_mobile1 = text15; monitormodel.upunit2 = text16; monitormodel.upcontact_name2 = text17; monitormodel.upcontact_mobile2 = text18; monitormodel.charvalue1 = images; monitormodel.coordinates_x = textx; monitormodel.coordinates_y = texty;
                    if (new monitor().Update(monitormodel))
                    {
                        Response.Write("<script>alert('监测点修改成功！');window.location = 'edmonitor.aspx?id=" + mindex + "';</script>");
                    }
                }
            }
            if (flag1 == 1 && flag2 == 2)
            {

                Model.monitor monitormodel = new monitor().GetModel(mindex);
                if (monitormodel != null)
                {
                    monitormodel.m_area_name = text1; monitormodel.addr = text3; monitormodel.type = text4; monitormodel.level = text5; monitormodel.population = Convert.ToInt32(text6); monitormodel.contact_name1 = text7; monitormodel.contact_mobile1 = text8; monitormodel.contact_name2 = text9; monitormodel.contact_mobile2 = text10; monitormodel.contact_name3 = text11; monitormodel.contact_mobile3 = text12; monitormodel.upunit1 = text13; monitormodel.upcontact_name2 = text14; monitormodel.upcontact_mobile1 = text15; monitormodel.upunit2 = text16; monitormodel.upcontact_name2 = text17; monitormodel.upcontact_mobile2 = text18; monitormodel.coordinates_x = textx; monitormodel.coordinates_y = texty;
                    if (new monitor().Update(monitormodel))
                    {
                        Response.Write("<script>alert('监测点修改成功！');window.location = 'edmonitor.aspx?id=" + mindex + "';</script>");
                    }
                }
            }
        }
    }
}